﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace Chat_D_LuX_Client
{
    /// <summary>
    /// Interaction logic for Register.xaml
    /// </summary>
    public partial class Register : Window
    {
        // Service Reference
        ChatServiceRef.Chat_ServiceClient sr = new ChatServiceRef.Chat_ServiceClient();

        private int _numErrorsOnScreen = 0;
        private Model.User _user = new Model.User();

        public Register()
        {
            InitializeComponent();
            regWinGrid.DataContext = _user;
            txtUsername.Focus();
        }

        private void Validation_Error(object sender, ValidationErrorEventArgs e)
        {
            if (e.Action == ValidationErrorEventAction.Added)
                _numErrorsOnScreen++;
            else
                _numErrorsOnScreen--;
        }

        private void RegisterUser_CanExecute(object sender, CanExecuteRoutedEventArgs e)
        {
            e.CanExecute = _numErrorsOnScreen == 0;
            e.Handled = true;
        }

        private void AddUser_Executed(object sender, ExecutedRoutedEventArgs e)
        {
            Model.User user = regWinGrid.DataContext as Model.User;

            // Call Register user service here.

            // Close Registration form and show main window
        }

        private void btnClear_Click(object sender, RoutedEventArgs e)
        {
            //clear all textboxes tried to use a loop to loop through controls but WPF doesn't like that
            txtEmail.Clear();
            txtUsername.Clear();
            txtPassword.Clear();
            txtPassword2.Clear();
            txtUsername.Focus();
        }

        private void btnRegister_Click(object sender, RoutedEventArgs e)
        {
            //TODO Register DAT ASS
            string username = txtUsername.Text.ToString();
            string password = txtPassword.Password.ToString();
            string passConfirm = txtPassword2.Password.ToString();
            string email = txtEmail.Text.ToString();

            if (password == passConfirm)
            {
                // Call service for registering user.
                sr.Register(username, password, email);
                MessageBox.Show("Registration Successful. Please log in.");
                this.Close();
            }
            else
            {
                txtPassword2.Clear();
                txtPassword.Clear();
                txtPassword.Focus();
                MessageBox.Show("Passwords do not match.");
            }
        }
    }
}
